package net.jumpengine.bean;

import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;

/**
 * 简单实现的图片绘制Base
 * <br /><br />
 * <b>作者:&nbsp;</b>张天文 <br />
 * <b>日期:&nbsp;</b>2020年2月24日 下午4:52:20<br />
 * @date
 */
public class ImageBase extends Base {
	protected Image image;
	protected boolean same;
	protected int cx=-1,cy=-1,cwidth=-1,cheight=-1;
	
	/**
	 * 默认为图片宽度和高度的构造方法
	 * @param x x坐标
	 * @param y y坐标
	 * @param image 图片
	 */
	public ImageBase(int x, int y,Image image) {
		this(x,y,(int)image.getWidth(),(int)image.getHeight(),0,image);
	}
	
	public ImageBase(int x, int y,int index,Image image) {
		this(x,y,(int)image.getWidth(),(int)image.getHeight(),index,image);
	}

	public ImageBase(int x, int y, int width, int height,Image image) {
		this(x,y,width,height,0,image);
	}
	public ImageBase(int x, int y, int width, int height,int index,Image image) {
		super(x, y, width, height,index);
		this.image=image;
		same=width==image.getWidth() && height==image.getHeight();
	}
	
	public ImageBase(int x, int y, int width, int height,int index,Image image,int cx,int cy,int cwidth,int cheight) {
		super(x, y, width, height,index);
		this.image=image;
		this.cx=cx;
		this.cy=cy;
		this.cwidth=cwidth;
		this.cheight=cheight;
	}
	
	public Image getImage() {
		return image;
	}
	public void setImage(Image image) {
		this.image = image;
	}
	
	@Override
	public void draw(GraphicsContext g) {
		if(!visible)return;
		if(same) {
			g.drawImage(image, x, y);	
		}else if(cx>0) {
			g.drawImage(image, cx, cy, cwidth, cheight, x, y, width, height);
		}else {
			double tmp;
			if(image.getWidth()<image.getHeight()) {
				tmp=width*height/image.getHeight();
				g.drawImage(image,x+(width-tmp)/2,y, tmp, height);
			}else {
				tmp=height*width/image.getWidth();
				g.drawImage(image,x,y+(height-tmp)/2, width, tmp);
			}
		}
	}
}
